home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Tools - Objects / MacsBug / MacsBug 6.2.1 / dcmds / C Samples / File.c < prev    next >
Text File  |  1991-05-01  |  6KB  |  249 lines

  1. /*     file.c
  2.     This is the FCB dcmd.
  3.  
  4.     Copyright © 1988 Apple Computer, Inc.  All rights reserved.
  5.  
  6.     Modification history:
  7.         29Nov88 sad        revised for new dcmd names.
  8.          5Oct88    sad        broke out formatting routines to put.c
  9.         30Sep88 sad        written
  10.  
  11.     The following MPW commands will build the dcmd and copy it to the
  12.     "Debugger Prefs" file in the System folder. The dcmd's name in
  13.     MacsBug will be the name of the file built by the Linker.
  14.     You must first copy dcmd.h, dcmdGlue.a.o and DRunTime.o from the
  15.     C Samples folder into this folder.
  16.  
  17.     C Put.c
  18.     C File.c
  19.     Link dcmdGlue.a.o File.c.o put.c.o DRuntime.o "{Libraries}"Interface.o -o File
  20.     BuildDcmd File 1003
  21.     Echo 'include "File";'    |    Rez -a -o "{systemFolder}Debugger Prefs"
  22. */
  23.  
  24. #include <Types.h>
  25. #include <Memory.h>
  26. #include <Files.h>
  27.  
  28. #include "dcmd.h"
  29. #include "put.h"
  30.  
  31. #define FSFCBLen 0x3f6
  32. #define FCBsPtr 0x34e
  33.  
  34. typedef struct FCB
  35.     {
  36.     unsigned long fcbFlNum;
  37.     unsigned char fcbMdRByt;
  38.     unsigned char fcbTypByt;
  39.     unsigned short fcbSBlk;
  40.     unsigned long fcbEOF;
  41.     unsigned long fcbPLen;
  42.     unsigned long fcbCrPs;
  43.     VCB* fcbVPtr;
  44.     void* fcbBfAdr;
  45.     unsigned short fcbFlPos;
  46.     unsigned long fcbClmpSize;
  47.     void* fcbBTCBPtr;
  48.     unsigned long fcbExtRec[3];
  49.     OSType fcbFType;
  50.     unsigned long fcbCatPos;
  51.     unsigned long fcbDirID;
  52.     char fcbCName[32];
  53.     } FCB;
  54.  
  55.  
  56. static void DrawHdr()
  57. {
  58. //                             1         2         3         4         5         6         7
  59. //                    1234567890123456789012345678901234567890123456789012345678901234567890
  60.     dcmdDrawLine("\pfRef File         Vol      Type Fl Fork    LEof    Mark  FlNum Parent FCB at");
  61. }
  62.  
  63.  
  64. static void DrawFCB(int fref, FCB* fcbp)
  65. {
  66.     PutUHexWord(fref);
  67.     PutSpace();
  68.     PutPStrTruncTo(fcbp->fcbCName,17);
  69.     PutSpace();
  70.     PutPStrTruncTo(fcbp->fcbVPtr->vcbVN,26);
  71.     PutSpace();
  72.     PutOSType(fcbp->fcbFType);
  73.     PutSpace();
  74.     PutChar((fcbp->fcbMdRByt & 0x80) ? 'D' : 'd');
  75.     PutChar((fcbp->fcbMdRByt & 0x01) ? 'W' : 'w');
  76.     PutSpace();
  77.     if (fcbp->fcbMdRByt & 0x02) PutPStr("\prsrc ");
  78.     else PutPStr("\pdata ");
  79.     PutUDecTo(fcbp->fcbEOF,47);
  80.     PutSpace();
  81.     PutUDecTo(fcbp->fcbCrPs,55);
  82.     PutSpace();
  83.     PutUHexZTo(fcbp->fcbFlNum,6,62);
  84.     PutSpace();
  85.     PutUHexZTo(fcbp->fcbDirID,6,69);
  86.     PutSpace();
  87.     PutUHexZTo((unsigned long)fcbp,6,76);
  88.     PutLine();
  89. } // DrawFCB
  90.  
  91.  
  92. static Boolean PrefixPStr(const Str255 astr, const Str255 bstr)
  93. // returns true if astr is equal to a prefix of bstr
  94. //    astr must not be longer than 31 characters
  95. {
  96.     char newstr[31];
  97.     int alen = *astr;
  98.     int blen = *bstr;
  99.     if (alen <= blen)
  100.         {
  101.         BlockMove(bstr+1,newstr+1,alen);
  102.         newstr[0] = alen;
  103.         return EqualString(astr,newstr,false,true);
  104.         }
  105.     else return false;    
  106. } // PrefixPStr
  107.  
  108. // EJECT
  109.  
  110. pascal void CommandEntry(dcmdBlock* paramPtr)
  111. {
  112.     switch (paramPtr->request)
  113.         {
  114.         case dcmdInit:
  115.             break;
  116.  
  117.         case dcmdHelp:
  118.             dcmdDrawLine("\pfile [fRefNum|\"file name\"]");
  119.             dcmdDrawLine("\p   Displays file information for the given fRefNum, file name or all open files.");
  120.             dcmdDrawLine("\p      Flags are D/d=Dirty, W/w=writeable.");
  121.             break;
  122.  
  123.         case dcmdDoIt:
  124.             {
  125.             Boolean doOneFCB = false;
  126.             long fref;
  127.             short c;
  128.             Boolean haveFileName = false;
  129.             Str255 filename;
  130.             int fcblen;                // the length of one fcb
  131.             char* fcbsbase;
  132.             int fcbslen;            // the length of the fcbs block
  133.  
  134.             dcmdSwapWorlds();
  135.  
  136.             dcmdDrawLine("\pDisplaying File Control Blocks");
  137.  
  138.           // get low-memory values after dcmdSwapWorlds()
  139.             fcblen = *(unsigned short*)FSFCBLen;
  140.             fcbsbase = *(char**)FCBsPtr;
  141.             fcbslen = *(unsigned short*)fcbsbase;
  142.  
  143.             if (fcblen != sizeof(FCB))
  144.                 {
  145.                 PutPStr("\FSFCBLen = ");
  146.                 PutUDec(fcblen);
  147.                 PutPStr("\p expected ");
  148.                 PutUDec(fcblen);
  149.                 PutLine(sizeof(FCB));
  150.                 }
  151.             if ((fcbslen - 2) % fcblen != 0)
  152.                 {
  153.                 PutPStr("\pbad fcbslen ");
  154.                 PutUHexWord(fcbslen);
  155.                 PutLine();
  156.                 }
  157.  
  158.             c = dcmdPeekAtNextChar();
  159.             if (c == '"' || c == '\'')
  160.                 {
  161.                 haveFileName = true;
  162.                 (void)dcmdGetNextParameter(filename);
  163.                 }
  164.             else (void)dcmdGetNextExpression(&fref, &doOneFCB);
  165.  
  166.             if (doOneFCB)
  167.                 {
  168.                 fref = (unsigned short)fref;
  169.                 if ((fref > fcbslen) || ((fref - 2) % fcblen != 0))
  170.                     {
  171.                     PutPStr("\pbad file refnum ");
  172.                     PutUHexWord(fref);
  173.                     PutLine();
  174.                     }
  175.                 else
  176.                     {
  177.                     FCB* fcbp = (FCB*)(fref + fcbsbase);
  178.                     if (fcbp->fcbFlNum)
  179.                         {
  180.                         DrawHdr();
  181.                         DrawFCB(fref,fcbp);
  182.                         }
  183.                     else
  184.                         {
  185.                         PutPStr("\pFCB ");
  186.                         PutUHexWord(fref);
  187.                         PutPStr("\p is not in use");
  188.                         PutLine();
  189.                         }
  190.                     }
  191.                 }
  192.             else
  193.                 {
  194.                 int numfcbs = (fcbslen - 2) / fcblen;
  195.                 int fcbsused = 0;
  196.                 Boolean foundOne = false;
  197.                 for (fref = 2;
  198.                      fref < fcbslen;
  199.                      fref += fcblen)
  200.                     {
  201.                     FCB* fcbp = (FCB*)(fcbsbase + fref);
  202.                     if (fcbp->fcbFlNum &&
  203.                         (!haveFileName || PrefixPStr(filename,fcbp->fcbCName)))
  204.                         {
  205.                         fcbsused++;
  206.                         if (!foundOne)
  207.                             {
  208.                             DrawHdr();
  209.                             foundOne = true;
  210.                             }
  211.                         DrawFCB(fref,fcbp);
  212.                         }
  213.                     if (paramPtr->aborted) break;
  214.                     }
  215.                 if (!paramPtr->aborted)
  216.                     if (haveFileName)
  217.                         {
  218.                         if (!foundOne)
  219.                             {
  220.                             PutPStr("\pno open files match \"");
  221.                             PutPStr(filename);
  222.                             PutChar('"');
  223.                             PutLine();
  224.                             }
  225.                         }
  226.                     else
  227.                         {
  228.                         PutUDec(numfcbs);
  229.                         PutPStr("\p FCBs, ");
  230.                         PutUDec(fcbsused);
  231.                         PutPStr("\p in use, ");
  232.                         PutUDec(numfcbs - fcbsused);
  233.                         PutPStr("\p free");
  234.                         PutLine();
  235.                         }
  236.                 }    
  237.  
  238.             dcmdSwapWorlds();
  239.             }
  240.             break;
  241.  
  242.         default:
  243.             PutPStr("\punknown request ");
  244.             PutUDec(paramPtr->request);
  245.             PutLine();
  246.             break;
  247.         }
  248. } // CommandEntry
  249.